/*
 *     This file is part of Neptune Network Emulator.
 *
 *  Neptune Network Emulator is free software: 
 *  you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  Neptune Network Emulator is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with Neptune Network Emulator.  If not, see <http://www.gnu.org/licenses/>.
 *
 */
/**
 *
 */
package neptune.web.client.commands;

import neptune.web.client.WebInterface;
import neptune.web.client.services.ClusterService;

import com.google.gwt.user.client.Command;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Label;

/**
 * @author Roberto Bifulco [info@RobertoBifulco.it] [RobertoBifulco.it]
 * 
 */
public class SaveNodeAsTemplateCommand implements Command {

	private String nodeId;

	/**
	 * @param nodeId
	 */
	public SaveNodeAsTemplateCommand(String nodeId) {
		super();
		this.nodeId = nodeId;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.google.gwt.user.client.Command#execute()
	 */
	public void execute() {

		String templateName = Window.prompt("Specify a template name",
				"templateName");
		if (templateName == null)
			return;

		// TODO aggiungere aggiornamenti all'interfaccia riguardo lo stato del
		// nodo
		ClusterService.Util.getInstance().saveNodeAsTemplate(nodeId,
				templateName, new AsyncCallback<String>() {

					public void onSuccess(String result) {
						WebInterface.showMessageDialog(new Label(
								"Template creation successfull. The node used as template is now in '"
										+ result + "' status"), false);
					}

					public void onFailure(Throwable caught) {
						WebInterface.addMessageToConsoleAndShowDialog(
								"Unable to create template, cause: "
										+ caught.getMessage(), true);
					}
				});
	}

}
